﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sbook
{
    public partial class UpdateUser : System.Web.UI.Page
    {
        protected string Username = "";
        protected static string Id = "";
        protected List<Data.UserInfo> listU = null;
        protected static string DId="";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Id = Request.QueryString["Id"];
                listU = Business.UserService.User_GetById(Id);
                grdShop.DataSource = this.GetListShop(Id);
                grdShop.DataBind();
            }
        }

        private List<Shopping.MyShop> GetListShop(string UId)
        {
            List<Data.DeliveryInfo> listD = Business.DeliveryService.Delivery_GetByAll().FindAll(d => d.UId == UId);
            //List<Data.CustomerInfo> listCAll = Business.CustomerService.Customer_GetByAll();
            List<Shopping.MyShop> listShop = new List<Shopping.MyShop>();
            for (int i = 0; i < listD.Count; i++)
            {
                Shopping.MyShop item = new Shopping.MyShop();
                item.ShopId = listD[i].DeliveryId;
                item.ShopCode ="#"+ listD[i].DeliveryCode;
                item.ShopDate =Convert.ToDateTime( listD[i].DeliveryDate).ToString("dd/MM/yyyy HH:mm");
                item.ShopStatus = listD[i].DeliveryStatus;
                switch (item.ShopStatus)
                {
                    case "0":
                        item.ShopStatus = "Chưa xử lý";
                        break;
                    case "1":
                        item.ShopStatus = "Đã xác nhận";
                        break;
                    case "2":
                        item.ShopStatus = "Chờ giao hàng";
                        break;
                    case "3":
                        item.ShopStatus = "Đã hoàn tất";
                        break;
                    case "4":
                        item.ShopStatus = "Đã hủy đơn hàng";
                        break;
                }

                item.ShopAddress = Business.DeliveryService.Delivery_GetByAll().FindAll(d => d.UId == UId)[0].DeliveryShippingPoint;
                item.ShopName = Business.DeliveryService.Delivery_GetByAll().FindAll(d => d.UId == UId)[0].DeliveryName;
                listShop.Add(item);
            }
            return listShop;
        }
        //protected void lbtView_Click(object sender, EventArgs e)
        //{
        //    grdShop.DataSource = this.GetListShop(Id);
        //    grdShop.DataBind();
        //}

        protected  LinkButton link = null;
        protected static string DId2 = "";
        protected static string Status = "";
        protected void lbtView_Click(object sender, EventArgs e)
        {
            string Id2 = Request.QueryString["Id"];
            listU = Business.UserService.User_GetById(Id2);
            grdShop.DataSource = this.GetListShop(Id2);
            grdShop.DataBind();


            link = sender as LinkButton;
             DId2 = link.CommandArgument.ToString();
             Status = link.CommandName;

            grdShopDetail.DataSource = Business.DeliveryDetailService.DeliveryDetail_GetByAll().FindAll(de => de.DeliveryId == DId2);
            grdShopDetail.DataBind();
            for (int i = 0; i < grdShopDetail.Items.Count; i++)
            {
                grdShopDetail.Items[i].Cells[1].Text = Business.ProductsService.Products_GetById(grdShopDetail.Items[i].Cells[1].Text)[0].PName;
                if (Status != "Chưa xử lý")
                {
                    grdShopDetail.Items[i].Cells[5].Visible = false;
                }
            }
           
            lblShopCode.Text += link.Text;
            lblShopDate.Text += Business.DeliveryService.Delivery_GetByAll().Find(d => d.DeliveryCode == lblShopCode.Text.Split('#')[1]).DeliveryDate;
            string s = Business.DeliveryService.Delivery_GetByAll().Find(d => d.DeliveryCode == lblShopCode.Text.Split('#')[1]).DeliveryStatus;
            switch (s)
            {
                case "0":
                    lblStatus.Text += "Chưa xử lý";
                    break;
                case "1":
                    lblStatus.Text += "Đã xác nhận";
                    break;
                case "2":
                    lblStatus.Text += "Chờ giao hàng";
                    break;
                case "3":
                    lblStatus.Text += "Đã hoàn tất";
                    break;
            }
            lblTongcong.Text = Business.DeliveryService.Delivery_GetByAll().Find(d => d.DeliveryCode == lblShopCode.Text.Split('#')[1]).DeliveryPrice.Split('.')[0]+" VNĐ";
            string UId = Business.DeliveryService.Delivery_GetByAll().Find(d => d.DeliveryCode == lblShopCode.Text.Split('#')[1]).UId;
            lblEmail.Text = Business.UserService.User_GetById(UId)[0].Email;
            lblMailTo.Text = Business.DeliveryService.Delivery_GetByAll().Find(d => d.DeliveryCode == lblShopCode.Text.Split('#')[1]).DeliveryEmail;
            lblPhoneTo.Text = Business.DeliveryService.Delivery_GetByAll().Find(d => d.DeliveryCode == lblShopCode.Text.Split('#')[1]).DeliveryPhone;
            lblFullNameTo.Text = Business.DeliveryService.Delivery_GetByAll().Find(d => d.DeliveryCode == lblShopCode.Text.Split('#')[1]).DeliveryName;
            lblAddressTo.Text = Business.DeliveryService.Delivery_GetByAll().Find(d => d.DeliveryCode == lblShopCode.Text.Split('#')[1]).DeliveryShippingPoint;
            pnTomtat.Visible = true;
            pnDelivery.Visible = true;
            pnViewShop.Visible = false;
            pnDetail.Visible = true;
        }

        /// <summary>
        /// Go
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtEdit_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < grdShopDetail.Items.Count; i++)
            {
                LinkButton link2 = (LinkButton)grdShopDetail.Items[i].FindControl("lbtEdit") as LinkButton;
                TextBox txtQty = (TextBox)grdShopDetail.Items[i].FindControl("txtQuantity") as TextBox;
                string[] arrCA = link2.CommandArgument.ToString().Split(':');
                Data.DeliveryDetailInfo item = new Data.DeliveryDetailInfo();
                item.DeliveryId = arrCA[0];
                item.ProductId = arrCA[1];
                item.Qty = txtQty.Text;
                item.Location = grdShopDetail.Items[i].Cells[4].Text;
                item.Remark = "";
                item.Price = grdShopDetail.Items[i].Cells[2].Text;
                Business.DeliveryDetailService.DeliveryDetail_Update(item);
            }
            
            string Id3 = Request.QueryString["Id"];
            listU = Business.UserService.User_GetById(Id3);
            grdShop.DataSource = this.GetListShop(Id3);
            grdShop.DataBind();

            grdShopDetail.DataSource = Business.DeliveryDetailService.DeliveryDetail_GetByAll().FindAll(de => de.DeliveryId == DId2);
            grdShopDetail.DataBind();
            for (int i = 0; i < grdShopDetail.Items.Count; i++)
            {
                grdShopDetail.Items[i].Cells[1].Text = Business.ProductsService.Products_GetById(grdShopDetail.Items[i].Cells[1].Text)[0].PName;
                if (Status != "Chưa xử lý")
                {
                    grdShopDetail.Items[i].Cells[5].Visible = false;
                }
            }
            pnViewShop.Visible = false;
            pnDetail.Visible = true;

        }
     /// <summary>
     /// sdfaga
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="e"></param>

        protected void lbtDelete_Click(object sender, EventArgs e)
        {
            LinkButton link3 = sender as LinkButton;
            string[] arrCA = link3.CommandArgument.ToString().Split(':');
            Business.DeliveryDetailService.DeliveryDetail_Delete(arrCA[0], arrCA[1]);

            string Id3 = Request.QueryString["Id"];
            listU = Business.UserService.User_GetById(Id3);
            grdShop.DataSource = this.GetListShop(Id3);
            grdShop.DataBind();

            grdShopDetail.DataSource = Business.DeliveryDetailService.DeliveryDetail_GetByAll().FindAll(de => de.DeliveryId == DId2);
            grdShopDetail.DataBind();
            for (int i = 0; i < grdShopDetail.Items.Count; i++)
            {
                grdShopDetail.Items[i].Cells[1].Text = Business.ProductsService.Products_GetById(grdShopDetail.Items[i].Cells[1].Text)[0].PName;
                if (Status != "Chưa xử lý")
                {
                    grdShopDetail.Items[i].Cells[5].Visible = false;
                }
            }
            pnViewShop.Visible = false;
            pnDetail.Visible = true;
        }
    }
}